package com.teach.service.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.teach.commons.PasswordUtil;
import com.teach.dao.LjDAO;
import com.teach.dto.LjSearch;
import com.teach.entity.Lj;
import com.teach.entity.User;
import com.teach.exception.PasswordErrorException;
import com.teach.exception.UserLockedException;
import com.teach.exception.UserNotExistsException;
import com.teach.service.LjService;
import com.teach.vo.LjVO;
import com.teach.vo.LoginUser;
import com.teach.vo.PageVO;

import cn.hutool.core.util.StrUtil;

public class LjServiceImpl implements LjService{
	
	private static Logger log = LoggerFactory.getLogger("LjServiceImpl.class");

	private LjDAO ljDAO = new LjDAO();
	
	@Override
	public List<Lj> list(int len) {
		List<Lj> list = new ArrayList<Lj>();
		try {
			List<Lj> list1 = ljDAO.select(0, len);//返回记录
			
			for (Lj entity : list1) {
				Lj lj = new Lj();
				lj.setLjId(entity.getLjId());
				lj.setLjName(entity.getLjName());
				lj.setLjNickname(entity.getLjNickname());
				lj.setLjPassword(entity.getLjPassword());
				lj.setLjSalt(entity.getLjSalt());
				lj.setLjPhone(entity.getLjPhone());
				lj.setLjQq(entity.getLjQq());
				lj.setLjSex(entity.getLjSex());
				lj.setLjBrithdate(entity.getLjBrithdate());
				lj.setLjAddressCode(entity.getLjAddressCode());
				lj.setLjAddress(entity.getLjAddress());
				lj.setLjLoves(entity.getLjLoves());
				lj.setLjStatus(entity.getLjStatus());
				list.add(lj);
			}
		} catch (SQLException e) {
//			e.printStackTrace();
			log.error(e.getMessage());
		}
		return list;
	}

	@Override
	public int delete(Long LjId) {
		try {
			return ljDAO.deleteById(LjId);
		} catch (SQLException e) {
			throw new RuntimeException("没有对应的记录",e);
		}
	}

	@Override
	public int insert(Lj lj) {
		
		String ljSalt = UUID.randomUUID().toString().replace("-", "");
		String ljPassword = PasswordUtil.encode(lj.getLjPassword(), ljSalt);
		
		lj.setLjPassword(ljPassword);
		lj.setLjSalt(ljSalt);
		
		try {
			return ljDAO.insert(lj);
		} catch (SQLException e) {
			throw new RuntimeException("添加用户失败",e);
		}
	}

	@Override
	public Lj get(Long ljId) {
		try {
			return ljDAO.selectById(ljId);
		} catch (SQLException e) {
			throw new RuntimeException("没有对应记录",e);
		}
	}

	@Override
	public int update(Lj lj) {
		try {
			Lj oldLj = ljDAO.selectById(lj.getLjId());
			
			oldLj.setLjId(lj.getLjId());
			oldLj.setLjNickname(lj.getLjNickname());
			oldLj.setLjSex(oldLj.getLjSex());
			oldLj.setLjPhone(lj.getLjPhone());
			oldLj.setLjQq(lj.getLjQq());
			oldLj.setLjBrithdate(lj.getLjBrithdate());
			
			return ljDAO.update(oldLj);
		} catch (SQLException e) {
			throw new RuntimeException("没有对应记录",e);
		}
	}

	@Override
	public List<Lj> list1(int current, int rowMax) {
		try {
			return ljDAO.select((current-1)*rowMax, rowMax);
		} catch (SQLException e) {
			throw new RuntimeException("没有对应记录",e);
		}
	}

	@Override
	public Long count() {
		try {
			return ljDAO.count();
		} catch (SQLException e) {
			throw new RuntimeException("没有对应记录",e);
		}
	}

	@Override
	public PageVO<Lj> page(int current, int rowMax) {
		try {
			PageVO<Lj> page = new PageVO<Lj>(ljDAO.count(), current, rowMax);
			return page.setList(ljDAO.select(page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			e.printStackTrace();// 日志
			throw new RuntimeException("获取用户分页数据失败", e);
		}
	}

	@Override
	public LoginUser login(String name, String password) {
		LoginUser user = new LoginUser();
		String salt = UUID.randomUUID().toString().replace("-", "");
		String psd= PasswordUtil.encode(password, salt);
		
		User u = new User();
		u.setUserName(name);;
		u.setUserSalt(salt);
		
		try {
			u = ljDAO.selectByModel(u);
			System.out.println("dao里查到的用户"+u);
		} catch (SQLException e) {
			throw new RuntimeException("用户不存在",e);
		}
		if(!u.getUserPassword().equals(PasswordUtil.encode(password, u.getUserSalt()))){
			throw new RuntimeException("密码错误");
		}
		if (u.getUserStatus().equals(1)) {
			throw new RuntimeException("用户被锁定");
		}
		user.setUserId(u.getUserId());
		user.setUserName(u.getUserName());
		user.setUserNickname(u.getUserNickname());
		
		return user;
	}

	
}
